IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_CreateUser]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_CreateUser]
GO
create procedure [dbo].[cms_sp_CreateUser]
@UserId int = null output,
@SiteId int = null,
@SiteGroupId int = null,
@LoginName nvarchar(150),
@UserEmail nvarchar(250),
@UserName nvarchar(250),
@PasswordHash nvarchar(150),
@PasswordSalt nvarchar(150) = null,
@IsSystem bit,
@CreatedBy int,
@DateCreated datetime
as
if @IsSystem is null
begin
set @IsSystem = 0
end
insert into [dbo].[Users]
(
[SiteId],
[SiteGroupId],
[LoginName],
[UserEmail],
[UserName],
[IsEnabled],
[IsSystem],
[CreatedBy],
[DateCreated]
)
values
(
@SiteId,
@SiteGroupId,
@LoginName,
@UserEmail,
@UserName,
1,
@IsSystem,
@CreatedBy,
@DateCreated
)
set @UserId = SCOPE_IDENTITY()
insert into [dbo].[UserPasswords]
(
[UserId],
[PasswordHash],
[PasswordSalt],
[IsLatestRevision],
[IsInitial],
[CreatedBy],
[DateCreated]
)
values
(
@UserId,
@PasswordHash,
@PasswordSalt, 
1,
1,
@CreatedBy,
@DateCreated
)
return @UserId


GO

